package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.entity.Address;
import com.cjh.common.response.Result;
import com.cjh.common.service.AddressService;
import com.cjh.common.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{
    @Autowired
    AddressMapper addressMapper;
    @Override
    public Result getAllAddress(Integer id) {
        List<Address> addresses = addressMapper.selectList(Wrappers.<Address>lambdaQuery().eq(Address::getUserId, id));
        return new Result().setCode(0).setMessage("获取成功!").setData(addresses);
    }

    @Override
    public Result addAddress(Address address) {
        clearDefaultAddress(address);
        return new Result().setCode(0).setMessage("新增成功!").setData(addressMapper.insert(address));
    }

    @Override
    public Result updateDefaultAddressById(Integer id,Integer defaultAddress,Integer userId) {
        if( defaultAddress == 1 ){
            Address address1 = addressMapper.selectOne(Wrappers.<Address>lambdaQuery().eq(Address::getUserId,userId).eq(Address::getDefaultAddress, 1));
            if(address1 != null){
                address1.setDefaultAddress(0);
                addressMapper.updateById(address1);
            }
        }
        Address address = new Address();
        address.setId(id);
        address.setDefaultAddress(defaultAddress);
        addressMapper.updateById(address);
        return  new Result().setCode(0).setMessage("修改成功!");
    }

    @Override
    public Result updateAddress(Address address) {
        clearDefaultAddress(address);
        return new Result().setCode(0).setMessage("修改成功!").setData(addressMapper.updateById(address));
    }

    private void clearDefaultAddress(Address address) {
        if( address.getDefaultAddress() == 1 ){
            Address address1 = addressMapper.selectOne(Wrappers.<Address>lambdaQuery().eq(Address::getUserId, address.getUserId()).eq(Address::getDefaultAddress, 1));
            if(address1 != null){
                address1.setDefaultAddress(0);
                addressMapper.updateById(address1);
            }
        }
    }

    @Override
    public Result getDeafultAddress(Integer userId) {
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Address::getUserId,userId);
        wrapper.eq(Address::getDefaultAddress,1);
        return new Result().setCode(0).setMessage("获取成功!").setData(addressMapper.selectOne(wrapper));
    }

    @Override
    public Result deleteAddress(Integer id) {
        return new Result().setCode(0).setMessage("删除成功!").setData(addressMapper.deleteById(id));
    }
}




